package org.jsrml.core.supplier.entity;

import java.util.Date;

import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.FetchType;
import javax.persistence.ForeignKey;
import javax.persistence.JoinColumn;
import javax.persistence.OneToOne;
import javax.persistence.Table;

import org.hibernate.annotations.DynamicUpdate;
import org.jsrml.common.persistence.StringIdBaseEntity;
import org.jsrml.common.util.UUIDGenerator;
import org.jsrml.core.base.entity.BaseStatus;
import org.jsrml.core.supplier.command.SaveContactCo;

@DynamicUpdate
@Entity
@Table(name = M.TABLE_PREFIX + "CONTACT")
@SuppressWarnings("serial")
public class Contact extends StringIdBaseEntity {

	@Column(name = "LINK_QQ", length = 64)
	private String linkQQ;

	@Column(name = "LINK_WECHAT", length = 64)
	private String linkWeChat;

	/**
	 * 主要联系人，0：是，1：不是
	 */
	@Column(name = "MAIN_LINK", length = 64)
	private Integer mainLink;

	@OneToOne(fetch = FetchType.LAZY)
	@JoinColumn(name = "VENDOR_ID", foreignKey = @ForeignKey(name = "none"))
	private Vendor vendor;

	@Column(name = "CONTACT_CODE", length = 64)
	private String contactCode;

	@Column(name = "CONTACT_NAME", length = 128)
	private String contactName;
	/**
	 * 职位
	 */
	@Column(name = "POSITION", length = 64)
	private String position;

	@Column(name = "TEL", length = 64)
	private String tel;

	@Column(name = "EMAIL", length = 128)
	private String email;

	/**
	 * 备注
	 */
	@Column(name = "REM",columnDefinition=M.TEXT_COLUMN)
	private String rem;
	
	/**
	 *是否关闭，0：启用，1：停用
	 */
	@Column(name = "IS_CLOSE", length = 4)
	private Integer isClose;
	
	/**
	 *停用人
	 */
	@Column(name = "CLOSED_BY", length = 32)
	private String closedBy;
	/**
	 *停用时间
	 */
	@Column(name = "CLOSED_DATE", columnDefinition=M.DATE_COLUMN)
	private Date closedDate;	
	
	private BaseStatus status;

	/**
	 * 关闭
	 * @param closedBy
	 */
	public void close(String closedBy){
		setClosedBy(closedBy);
		setClosedDate(new Date());
		setIsClose(1);
	}
	
	public void create(SaveContactCo command, Vendor vendor) {
		setId(UUIDGenerator.getUUID());

		setVendor(vendor);
		setPosition(command.getPosition());
		setContactName(command.getContactName());
		setContactCode(command.getContactCode());
		setTel(command.getTel());
		setEmail(command.getEmail());
		setLinkQQ(command.getLinkQQ());
		setLinkWeChat(command.getLinkWeChat());
		setMainLink(command.getMainLink());
		setRem(command.getRem());
		setIsClose(0);
		setStatus(new BaseStatus());
		getStatus().setRemove(false);
		getStatus().setCreateDate(new Date());
	}

	public void modify(SaveContactCo command, Vendor vendor) {

		setVendor(vendor);
		setPosition(command.getPosition());
		setContactName(command.getContactName());
		setTel(command.getTel());
		setEmail(command.getEmail());
		setLinkQQ(command.getLinkQQ());
		setLinkWeChat(command.getLinkWeChat());
		setMainLink(command.getMainLink());
		setRem(command.getRem());
		if (getStatus() == null) {
			setStatus(new BaseStatus());
		}
		getStatus().setUpdateDate(new Date());
	}

	public void remove() {
		getStatus().setRemove(true);
	}

	public Vendor getVendor() {
		return vendor;
	}

	public void setVendor(Vendor vendor) {
		this.vendor = vendor;
	}

	public String getPosition() {
		return position;
	}

	public void setPosition(String position) {
		this.position = position;
	}

	public String getTel() {
		return tel;
	}

	public void setTel(String tel) {
		this.tel = tel;
	}

	public String getEmail() {
		return email;
	}

	public void setEmail(String email) {
		this.email = email;
	}

	public BaseStatus getStatus() {
		return status;
	}

	public void setStatus(BaseStatus status) {
		this.status = status;
	}

	public String getLinkQQ() {
		return linkQQ;
	}

	public void setLinkQQ(String linkQQ) {
		this.linkQQ = linkQQ;
	}

	public String getLinkWeChat() {
		return linkWeChat;
	}

	public void setLinkWeChat(String linkWeChat) {
		this.linkWeChat = linkWeChat;
	}

	public Integer getMainLink() {
		return mainLink;
	}

	public void setMainLink(Integer mainLink) {
		this.mainLink = mainLink;
	}

	public String getContactCode() {
		return contactCode;
	}

	public void setContactCode(String contactCode) {
		this.contactCode = contactCode;
	}

	public String getContactName() {
		return contactName;
	}

	public void setContactName(String contactName) {
		this.contactName = contactName;
	}

	public String getRem() {
		return rem;
	}

	public void setRem(String rem) {
		this.rem = rem;
	}

	public String getClosedBy() {
		return closedBy;
	}

	public void setClosedBy(String closedBy) {
		this.closedBy = closedBy;
	}

	public Date getClosedDate() {
		return closedDate;
	}

	public void setClosedDate(Date closedDate) {
		this.closedDate = closedDate;
	}

	public Integer getIsClose() {
		return isClose;
	}

	public void setIsClose(Integer isClose) {
		this.isClose = isClose;
	}

}
